//代码未通过
#include <unistd.h>
#include <iostream>
#define K 1024
#define WRITELEN (128*K)
using namespace std;
int main() {
    int result = -1;
    int fd[2],nbytes;
    pid_t pid;
    char str_info[WRITELEN] = "Hello Pipe";
    char readbuffer[10*K];
    int *reader_fd = &fd[0];
    int *writer_fd = &fd[1];
    result = pipe(fd);
    if(-1 == result)
    {
        cout<<"建立管道失败\n";
        return -1;
    }
    pid = fork();
    if(-1 == pid)
    {
        cout<<"fork进程失败\n";
        return -1;
    }
    if(0 == pid)
    {
        int write_size = WRITELEN;
        result = 0;
        close(*reader_fd);
        while(write_size >= 0)
        {
            result = write(*writer_fd,str_info,write_size);
            if(result > 0)
            {
                write_size -= result;
                cout<<"写入"<<result<<"个数据，剩下"<<write_size<<"个数据\n";
            }
            else
                sleep(10);
        }
        return 0;
    }
    else{
        close(*writer_fd);
        while(true)
        {
            nbytes = read(*reader_fd,readbuffer, sizeof(readbuffer));
            if(nbytes <= 0){
                cout<<"没有数据写入了\n";
                break;
            }
            cout<<"接收到"<<nbytes<<"个数据，内容为:"<<readbuffer<<"\n";
        }
    }
    return 0;
}